package com.srs.backend.serviceImpl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.srs.backend.mapper.FoodMapper;
import com.srs.backend.mapper.FoodMaterialMapper;
import com.srs.backend.mapper.FoodStyleMapper;
import com.srs.backend.pojo.FoodStyle;
import com.srs.backend.service.FoodStyleService;
import com.srs.backend.utils.Result;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;
import java.util.Map;

@Service
public class FoodStyleServiceImpl implements FoodStyleService {
    @Resource
    private FoodStyleMapper foodStyleMapper;

    @Override
    public Result add(Map<String, String> data) {
        String name = data.get("name");
//        String remark = data.get("remark");
        if (name == null || name.length() == 0) {
            return Result.error("菜品类型不能为空");
        }
        if (name.length() > 20) {
            return Result.error("菜品类型名称不能过长");
        }
        QueryWrapper<FoodStyle> foodStyleQueryWrapper = new QueryWrapper<>();
        foodStyleQueryWrapper.eq("name", name);
        List<FoodStyle> foodStyles = foodStyleMapper.selectList(foodStyleQueryWrapper);
        if (!foodStyles.isEmpty()) {
            return Result.error("当前菜品类型已经存在");
        }
        foodStyleMapper.insert(new FoodStyle(null, name));

        return Result.ok("success");
    }

    @Override
    public Result delete(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        FoodStyle curFoodStyle = foodStyleMapper.selectById(id);
        if (curFoodStyle == null) {
            return Result.error("当前菜品类型不存在或已被删除");
        }
        foodStyleMapper.deleteById(curFoodStyle);
        return Result.ok();
    }

    @Override
    public Result modify(Map<String, String> data) {
        String idStr = data.get("id");
        Integer id = null;
        try {
            id = Integer.parseInt(idStr);
        } catch (NumberFormatException e) {
            return Result.error("id数据有误");
        }
        FoodStyle curFoodStyle = foodStyleMapper.selectById(id);
        if (curFoodStyle == null) {
            return Result.error("当前菜品类型不存在或已被删除");
        }


        String name = data.get("name");
        if (name == null || name.length() == 0) {
            return Result.error("菜品类型名称不能为空");
        }
        if (name.length() > 20) {
            return Result.error("菜品类型名称不能过长");
        }
        QueryWrapper<FoodStyle> foodStyleQueryWrapper = new QueryWrapper<>();
        foodStyleQueryWrapper.eq("name", name);
        List<FoodStyle> foodStyles = foodStyleMapper.selectList(foodStyleQueryWrapper);
        if (!foodStyles.isEmpty()) {
            return Result.error("当前菜品类型已经存在");
        }

        curFoodStyle.setName(name);

        foodStyleMapper.updateById(curFoodStyle);
        return Result.ok();
    }

    @Override
    public Result getAll() {
        return Result.ok(foodStyleMapper.selectList(null));
    }
}
